package com.wy2cloud.sample.zuulandauthserver.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class SecurityUser extends AuthorizeUser implements UserDetails
{
    

	private static final long serialVersionUID = 1L;
	
	private List<AuthorizeRole> roles;
	private List<AuthorizeAuthority> authorities;
	
	public SecurityUser(AuthorizeUser user,List<AuthorizeRole> roles,List<AuthorizeAuthority> authorities) {
		if(user != null)
		{
			this.setId(user.getId());
			this.setAccount(user.getAccount());
			this.setBirthday(user.getBirthday());
			this.setSex(user.getSex());
			this.setPhone(user.getPhone());
			this.setName(user.getName());
			this.setEmail(user.getEmail());
			this.setPassword(user.getPassword());
			this.setCreateTime(user.getCreateTime());
			this.setUserLock(user.getUserLock());
			
			this.roles=roles;
			this.authorities=authorities;
			//this.setAuthorizeUserRoles(user.getAuthorizeUserRoles());
		}else {
		    this.roles=roles;
		    this.authorities=authorities;
		}
		
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		
		Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
		//Set<AuthorizeUserRole> userRoles = this.getAuthorizeUserRoles();
		
		if(authorities != null)
		{
			for (AuthorizeAuthority authoritiy : authorities) {
				SimpleGrantedAuthority authority = new SimpleGrantedAuthority(authoritiy.getId());
				grantedAuthorities.add(authority);
			}
		}
		return grantedAuthorities;
	}
	
	

	public List<AuthorizeRole> getRoles() {
		return roles;
	}

	@JsonIgnore
	@Override
	public String getPassword() {
		return super.getPassword();
	}

	@Override
	public String getUsername() {
		return super.getName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
	
}
